package com.yrcm.eduapp.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.yrcm.eduapp.R;
import com.yrcm.eduapp.ui.CoverFlowGL;

public class MSKCActivity extends LoginActivity implements OnClickListener {
	private int[] SAMPLE_IMAGES = new int[] { R.drawable.yuwen,
			R.drawable.shuxue, R.drawable.yingyu, R.drawable.wuli,
			R.drawable.huaxue, R.drawable.shengwu, R.drawable.zhengzhi,
			R.drawable.lishi, R.drawable.dili, R.drawable.qita };

	private int[] IMAGES_NAME = new int[] { R.drawable.mingcheng_yuwen,
			R.drawable.mingcheng_shuxue, R.drawable.mingcheng_yingyu,
			R.drawable.mingcheng_wuli, R.drawable.mingcheng_huaxue,
			R.drawable.mingcheng_shengwu, R.drawable.mingcheng_zhengzhi,
			R.drawable.mingcheng_lishi, R.drawable.mingcheng_dili,
			R.drawable.mingcheng_qita };

	private CoverFlowGL mCoverFlow;

	private LinearLayout layout;
	private ImageView mcImg;
	private ImageButton leftBtn, rightBtn;
	private ImageButton bfjlBtn;
	private int currP = 0;

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				mcImg.setBackgroundResource(IMAGES_NAME[msg.arg1]);
//				Intent intent = new Intent(MSKCActivity.this,
//						MSKCBaoActivity.class);
//				intent.putExtra("data", msg.arg1);
//				startActivity(intent);
				break;
			default:
				break;
			}
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mskc);

		initView();
		fillView();
	}

	public void initView() {
		layout = (LinearLayout) findViewById(R.id.cverflow);
		mCoverFlow = new CoverFlowGL(this);
		mcImg = (ImageView) findViewById(R.id.mingcheng_img);
		leftBtn = (ImageButton) findViewById(R.id.left_one);
		rightBtn = (ImageButton) findViewById(R.id.right_one);
		bfjlBtn = (ImageButton) findViewById(R.id.bofangjl_btn);

		bfjlBtn.setVisibility(View.VISIBLE);
	}

	public void fillView() {
		leftBtn.setOnClickListener(this);
		rightBtn.setOnClickListener(this);
		bfjlBtn.setOnClickListener(this);

		mCoverFlow.setCoverFlowListener(new CoverFlowGL.CoverFlowListener() {
			@Override
			public int getCount(CoverFlowGL view) {
				return SAMPLE_IMAGES.length;
			}

			@Override
			public Bitmap getImage(CoverFlowGL anotherCoverFlow, int position) {
				return BitmapFactory.decodeResource(getResources(),
						SAMPLE_IMAGES[position]);
			}

			@Override
			public void tileOnTop(CoverFlowGL view, int position) {
				Message msg = new Message();
				msg.what = 0;
				msg.arg1 = position;
				mHandler.sendMessage(msg);
				currP = position;
			}

			@Override
			public void topTileClicked(CoverFlowGL view, int position) {
				Intent intent = new Intent(MSKCActivity.this,
						MSKCBaoActivity.class);
				intent.putExtra("data", position);
				startActivity(intent);
			}

			@Override
			public void onTileClicked(CoverFlowGL view, int position) {
				// System.out.println("==========" + position + "--------");
				view.setSelection(position);
			}
		});

		mCoverFlow.setZOrderOnTop(true);
		layout.addView(mCoverFlow);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.left_one:
			System.out.println(currP + "===left_one===currP=======");
			mCoverFlow.setSelection(--currP);
			break;
		case R.id.right_one:
			System.out.println(currP + "===right_one===currP=======");
			mCoverFlow.setSelection(++currP);
			break;
		case R.id.bofangjl_btn:
			Intent intent = new Intent(this, BofangJiluActivity.class);
			startActivity(intent);
			break;
		default:
			break;
		}

	}

	@Override
	protected void onResume() {
		super.onResume();
		mCoverFlow.setVisibility(View.VISIBLE);
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	protected void onPause() {
		super.onPause();
		mCoverFlow.setVisibility(View.GONE);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			showExit();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
